/****************************************************************************
*  Arthea Server by R. Jennings (2007-2008)   http://arthea.googlecode.com/ *
*  By using this code you comply with the Artistic and GPLv2 Licenses.      *
****************************************************************************/


using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using Arthea.Connections.Players;
using Arthea.Globals;

namespace Arthea
{
    /// <summary>
    /// Implements a way to write a log
    /// </summary>
    public struct Log
    {
        /// <summary>
        /// Creates the name of the file.
        /// </summary>
        /// <returns>the file name</returns>
        private static string FileName
        {
            get { return Paths.LogDir + DateTime.Now.ToString("ddMMyy-h.mt") + ".log"; }
        }

        private static void LogMessage(string type, object message)
        {
            message = string.Format("[{0}] [{1,-5}] {2}", DateTime.Now, type, message);
            StreamWriter file = File.AppendText(FileName);
            file.WriteLine(message);
            file.Flush();
            file.Close();
            Console.WriteLine(message);
        }

        /// <summary>
        /// Logs a debugging message.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="args">The args.</param>
        public static void Debug(string text, params object[] args)
        {
            LogMessage("DEBUG", string.Format(text, args));
        }

        /// <summary>
        /// Logs a debugging message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Debug(object message)
        {
            LogMessage("DEBUG", message);
        }

        /// <summary>
        /// Logs a warning message.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="args">The args.</param>
        public static void Warn(string text, params object[] args)
        {
            LogMessage("WARN", string.Format(text, args));
        }

        /// <summary>
        /// Logs a warning message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Warn(object message)
        {
            LogMessage("WARN", message);
        }

        /// <summary>
        /// Logs and info message.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="args">The args.</param>
        public static void Info(string text, params object[] args)
        {
            LogMessage("INFO", string.Format(text, args));
        }

        /// <summary>
        /// Logs an info message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Info(object message)
        {
            LogMessage("INFO", message);
        }

        /// <summary>
        /// Logs an error message.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="args">The args.</param>
        public static void Error(string text, params object[] args)
        {
            LogMessage("ERROR", AddMethodName(string.Format(text, args)));
        }

        /// <summary>
        /// Logs an error message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Error(object message)
        {
            LogMessage("ERROR", AddMethodName(message));
        }

        /// <summary>
        /// Logs a fatal error message.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="args">The args.</param>
        public static void Fatal(string text, params object[] args)
        {
            LogMessage("FATAL", AddMethodName(string.Format(text, args)));
        }

        /// <summary>
        /// Logs a fatal error message.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Fatal(object message)
        {
            LogMessage("FATAL", AddMethodName(message));
        }

        /// <summary>
        /// Logs bugs.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="args">The args.</param>
        public static void Bug(string text, params object[] args)
        {
            string message = string.Format(text, args);
            LogMessage("BUG", AddMethodName(message));
            BugNet(message);
        }

        /// <summary>
        /// Log bugs.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void Bug(object message)
        {
            LogMessage("BUG", AddMethodName(message));
            BugNet(message);
        }

        /// <summary>
        /// Report a bugs to online admins.
        /// </summary>
        /// <param name="text">The text.</param>
        private static void BugNet(object text)
        {
            foreach (Player player in Lists.Players)
            {
                if (player.AdminPowers[Powers.BugNet])
                {
                    player.WriteLine("BUGNET> {0}", text);
                }
            }
        }

        private static object AddMethodName(object message)
        {
            StackTrace t = new StackTrace();

            if (t.FrameCount <= 1)
                return message;

            return string.Format("({0}) {1}", t.GetFrame(2).GetMethod().Name, message);
        }
    }
}